<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\controller\Admin;
use think\Db;

class Goods extends Admin {

    /**
     * @title 回收站还原
     */
    public function recycle_return($ids) {

        empty($ids) && $this->error('没有选中项');

        $affect_rows = db('goods')->where('id', 'in', $ids)->setField('delete_time', NULL);
        if ($affect_rows) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 回收站删除
     */
    public function recycle_remove($ids) {

        empty($ids) && $this->error('没有选中项');

        $affect_rows = db('goods')->where('id', 'in', $ids)->delete();
        if ($affect_rows) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 回收站清空
     */
    public function recycle_clear() {

        $affect_rows = db('goods')->where('delete_time', 'not null')->delete();
        if ($affect_rows) {
            $this->success('');
        } else {
            $this->error('清空失败');
        }
    }

    /**
     * @title 商品回收站
     */
    public function recycle() {


        $lists = model('goods')->model_where()->onlyTrashed()->order('id desc')->paginate(config('base.page_size'), false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());

        // 分类转移 
        $this->assign('category_select', model('goods_cat')->lists_select_tree());
        builder('list')
                ->addItem('id', '#')
                ->addItem('category_name', '分类')
                ->addItem('image', '主图', 'image')
                ->addItem('title', '名称')
                ->addItem('price', '价格')
                ->addItem('quantity', '数量')
                ->addItem('recommend', '推荐')
                ->addItem('status_text', '状态')
                ->build();
        return view();
    }

    /**
     * @title 商品框架
     */
    public function iframe() {
        // 加载内容
        $this->assign('iframe_src', url('admin/goods/index'));
        // 加载分类
        $this->assign('nodes', model('goods_cat')->iframe_category_tree_json());
        return $this->fetch('base:iframe');
    }

    /**
     * @title 商品列表
     */
    public function index() {


        $cid = input('get.category', null);

        session('redirect', url('index') . '?' . http_build_query(request()->get()));

        $lists = model('goods')->model_where()->order('recommend desc,listorder asc,id desc')->paginate(config('base.page_size'), false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());

        // 分类转移 
        $this->assign('category_select', model('goods_cat')->lists_select_tree());
        builder('list')
                ->addItem('id', '#')
                ->addItem('category_name', '分类')
                ->addItem('image', '主图', 'image')
                ->addItem('title', '名称')
                ->addItem('price', '价格')
                ->addItem('quantity', '数量')
                ->addItem('recommend', '推荐')
                ->addItem('pay_points', '积分商品')
                ->addItem('status_text', '状态')
                ->addSortItem('listorder', '排序', 'goods')
                ->addAction('评价', 'evaluate', '<i class="layui-icon layui-icon-dialogue"></i>', 'openbox_big')
                ->addAction('编辑', 'edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal')
                ->addAction('删除', 'delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();
        return view();
    }

    /**
     * @title 商品评价
     */
    public function evaluate($id) {

        empty($id) && $this->error('idmiss');

        $lists = model('goods_evaluate')->model_where()->where('a.goods_id', $id)->paginate(config('base.page_size'), false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('pages', $lists->render());

        builder('list')
                ->addItem('id', '#')
                ->addItem('nickname', '评价人')
                ->addItem('avatar', '头像', 'image')
                ->addItem('create_time', '评价时间')
                ->addItem('content', '评价内容')
                ->addItem('rank', '分值')
                ->addItem('status', '状态')
                ->addAction('编辑', 'evaluate_edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal')
                ->addAction('删除', 'evaluate_delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();

        return view();
    }

    public function evaluate_set_status() {

        $value = input('get.value', 0);
        $ids = input('post.ids');

        empty($ids) && $this->error('请选择要操作的数据');


        $affect_rows = db('goods_evaluate')->where('id', 'in', $ids)->setField('status', $value);
        $affect_rows ? $this->success('完成') : $this->error('没有更新任何数据');
    }

    /**
     * @title 评价编辑
     * @param type $id
     */
    public function evaluate_edit($id) {
        
    }

    /**
     * @title 商品添加
     */
    public function add() {

        if (request()->isPost()) {

 

            $message = model('goods')->add_submit(request()->post(), ['albums_update']);
            if ($message) {
                $this->error($message);
            } else {
                $this->success('', session('redirect'));
            }
        } else {


            $cid = input('get.cid', 0);


            builder('form')
                    ->addItem('cid', 'select', '分类', model('goods_cat')->lists_select_tree())
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('image', 'image', '图片', '', '', '', '200x200')
                    ->addItem('albums', 'albums', '图集', [])
                    ->addItem('price', 'input', '价格<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('price_market', 'input', '市场价<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('keywords', 'input', '关键词')
                    ->addItem('description', 'textarea', '描述')
                    ->addItem('points', 'input', '购买赠送积分')
                    ->addItem('pay_points', 'input', '兑换所需积分','','','','如果大于0，系统会把此商品当成积分商品')
                    ->addItem('quantity', 'input', '库存数量')
                    ->addItem('subtract', 'radio', '下单减存存?',  [1 => '是', 0 => '否'])
                    ->addItem('alias', 'input', '别名')
                    ->addItem('status', 'radio', '状态', [1 => '上架', 0 => '下架'])
                    ->addItem('tpl_views', 'input_select', '模板', 'admin/index/tpl_select')
                    ->addItem('content', 'umeditor', '内容', '', 'style="height: 400px;"', 'simple')
                    ->addItem('smeta_json', 'smeta_json', '补充字段', config('smeta_json_goods'))
                    ->build(['cid' => $cid, 'status' => 1]);
            return view();
        }
    }

    /**
     * @title 商品编辑
     */
    public function edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {




            $message = model('goods')->edit_submit(request()->post(), $id, ['albums_update']);
            if ($message) {
                $this->error($message);
            } else {
                $this->success('', session('redirect'));
            }
        } else {

            $one = db('goods')->where('id', $id)->find();
            builder('form')
                    ->addItem('cid', 'select', '分类', model('goods_cat')->lists_select_tree())
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('image', 'image', '图片', '', '', '', '200x200')
                    ->addItem('albums', 'albums', '图集', db('goods_albums')->where('goods_id', $id)->select())
                    ->addItem('price', 'input', '价格<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('price_market', 'input', '市场价<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('keywords', 'input', '关键词')
                    ->addItem('description', 'textarea', '描述')
                    ->addItem('points', 'input', '购买赠送积分')
                    ->addItem('pay_points', 'input', '兑换所需积分','','','','如果大于0，系统会把此商品当成积分商品')
                    ->addItem('quantity', 'input', '库存数量')
                    ->addItem('subtract', 'radio', '下单减存存?',  [1 => '是', 0 => '否'])
                    ->addItem('alias', 'input', '别名')
                    ->addItem('status', 'radio', '状态', [1 => '上架', 0 => '下架'])
                    ->addItem('tpl_views', 'input_select', '模板', 'admin/index/tpl_select')
                    ->addItem('content', 'umeditor', '内容', '', 'style="height: 400px;"', 'simple')
                    ->addItem('smeta_json', 'smeta_json', '补充字段', config('smeta_json_goods'))
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 商品删除
     */
    public function delete($id) {
        empty($id) && $this->error('参数不能为空');
        if (db('goods')->where('id', $id)->setField('delete_time', time()) !== FALSE) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 分类转移 
     */
    public function set_category() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {
            $affect_rows = db('goods')->where('id', 'in', $ids)->setField('cid', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

    /**
     * @title 商品分类
     */
    public function category() {

        $lists = model('goods_cat')->select();

        // 转成树形
        $lists = collection($lists)->toArray();
        $tree = new \utils\Tools\Tree($lists);

        $this->assign('lists', $tree->getArray(0));



        builder('list')
                ->addItem('id', '#')
                ->addSortItem('listorder', '排序', 'goods_cat')
                ->addItem('title', '标题')
                ->addItem('tpl_lists:tpl_views', '列表/详情')
                ->addItem('path', '路径')
                ->addItem('alias', '别名')
                ->addAction('编辑', 'category_edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal')
                ->addAction('删除', 'category_delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();
        return view();
    }

    /**
     * @title 商品分类添加
     */
    public function category_add() {

        if (request()->isPost()) {
            $post = input('post.');

            if ($insert_id = db('goods_cat')->insertGetId($post)) {

                //添加PATH信息
                $pid = $post['pid'] == 0 ? "0" : $post['pid'];
                if (empty($pid)) {
                    $path = "0-$insert_id";
                } else {
                    $parent = db('goods_cat')->where("id", $pid)->find();
                    $path = $parent['path'] . "-$insert_id";
                }
                db('goods_cat')->where('id', $insert_id)->setField('path', $path);
                //添加PATH信息结束

                $this->success('', url('category'));
            } else {
                $this->error('添加失败');
            }
        } else {
            builder('form')
                    ->addItem('pid', 'select', '上级', model('goods_cat')->lists_select_tree())
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('tpl_lists', 'input_select', '列表', 'admin/index/tpl_select')
                    ->addItem('tpl_views', 'input_select', '详情', 'admin/index/tpl_select')
                    ->addItem('keywords', 'input', '关键词')
                    ->addItem('description', 'input', '描述')
                    ->addItem('alias', 'input', '别名')
                    ->build();
            return view();
        }
    }

    /**
     * @title 商品分类
     */
    public function category_edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {
            $post = input('post.');

            //编辑PATH信息
            $pid = empty($post['pid']) ? "0" : $post['pid'];
            if (empty($pid)) {
                $post['path'] = "0-" . $post['id'];
            } else {
                $parent = db('goods_cat')->where("id", $pid)->find();
                $post['path'] = $parent['path'] . "-" . $post['id'];
            }
            //编辑PATH信息结束
            if (db('goods_cat')->where('id', $id)->update($post) !== FALSE) {
                $this->success('', url('category'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $one = db('goods_cat')->where('id', $id)->find();
            builder('form')
                    ->addItem('pid', 'select', '上级', model('goods_cat')->lists_select_tree(['id' => ['neq', $id]]))
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('tpl_lists', 'input_select', '列表', 'admin/index/tpl_select')
                    ->addItem('tpl_views', 'input_select', '详情', 'admin/index/tpl_select')
                    ->addItem('keywords', 'input', '关键词')
                    ->addItem('description', 'input', '描述')
                    ->addItem('alias', 'input', '别名')
                    ->build($one);
            return view();
        }
    }

    /**
     * 删除指定资源
     */
    public function category_delete($id) {

        empty($id) && $this->error('参数不能为空');

        if (db('goods_cat')->where('pid', $id)->count()) {
            $this->error('请先删除子类');
        } elseif (db('goods_cat')->where('id', $id)->delete() !== FALSE) {
            $this->success('', url('category'));
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 推荐 
     */
    public function set_recommend() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {
            $affect_rows = db('goods')->where('id', 'in', $ids)->setField('recommend', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

}
